package com.jimet.group;

import java.sql.ResultSet;
import java.util.Vector;
import com.jimet.db.JimetGroupDB;

public class JimetGroups {

	private int pos;
	public Vector<JimetGroup> groupVec;
	
	public JimetGroups(){
	}

	public int size()
	{
		if(this.groupVec == null)
			return 0;
		else
			return this.groupVec.size();
	}
	

	
	public void load()  throws Exception 
	{
		this.pos = -1;
		this.groupVec = new Vector<JimetGroup>();
		JimetGroupDB jimdb = new JimetGroupDB();	
		ResultSet rs = jimdb.getAllGroupSet();
		while(rs.next())
		{
			JimetGroup jgroup = new JimetGroup();
			jgroup.load(String.valueOf(rs.getInt(1)));
			this.groupVec.add(jgroup);
		}
		jimdb.disconnect();

	}

	public void search(String strSort, String strLike,String strSortBy,String strSearchBy)  throws Exception 
	{
		this.pos = -1;
		this.groupVec = new Vector<JimetGroup>();
		JimetGroupDB jimdb = new JimetGroupDB();	
		ResultSet rs = jimdb.getGroupSearchResults(strSort, strLike, strSortBy, strSearchBy);
		while(rs.next())
		{
			JimetGroup jgroup = new JimetGroup();
			jgroup.setID(rs.getInt(1));
			jgroup.setName(rs.getString(2));
			jgroup.setDescription(rs.getString(3));
			this.groupVec.add(jgroup);
		}
		jimdb.disconnect();

	}

	public void loadByGroupIDs(String [] strArrUserIDs) throws Exception
	{
		this.pos = -1;
		this.groupVec = new Vector<JimetGroup>();
		JimetGroupDB jimdb = new JimetGroupDB();		
		ResultSet rs = jimdb.getGroupsByID(strArrUserIDs);
		while(rs.next())
		{
			JimetGroup jgroup = new JimetGroup();
			jgroup.setID(rs.getInt(1));
			jgroup.setName(rs.getString(2));
			jgroup.setDescription(rs.getString(3));
			this.groupVec.add(jgroup);
		}
		jimdb.disconnect();
 	}
	public boolean contains(JimetGroup lookFor)
	{
		if(this.groupVec != null)
		{
			for(int i=0;i<this.groupVec.size();i++)
			{
				JimetGroup anObject = (JimetGroup) this.groupVec.elementAt(i);
				if(anObject.getID() == lookFor.getID())
					return true;
			}
		}
		return false;
	}

	public void delete() throws Exception
	{
		for(int i=0;i<groupVec.size();i++)
		{
			JimetGroup jgroup = (JimetGroup) groupVec.elementAt(i);
			jgroup.delete();
		}
	}

	public void addGroup(JimetGroup jgroup)
	{

		if(this.groupVec == null)
		{
			this.pos = -1;
			this.groupVec = new Vector<JimetGroup>();
		}
		this.groupVec.add(jgroup);
	}
	
	
	public boolean hasGroup(JimetGroup jgroup)
	{
		if(this.groupVec == null)
			return false;
		
		for(int i=0;i<this.groupVec.size();i++)
		{
			JimetGroup jg = (JimetGroup) this.groupVec.elementAt(i);
			if(jgroup.getID() == jg.getID())
				return true;
		}
		return false;
	}
	
	public void reset()
	{
		this.pos = -1;
	}

	public boolean next()
	{
		if(this.groupVec == null)
			return false;
		pos++;
		return  (pos < this.groupVec.size());
	}

	public JimetGroup getNext()
	{
		return (JimetGroup) this.groupVec.elementAt(this.pos);
	}
	

}
